﻿using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using Sinacor.Infra.UI.Common;
using Sinacor.UI.Bovespa.Ordens.Model;
using Sinacor.Infra.UI.Security.Services;
using Sinacor.UI.Bovespa.Ordens.Model.Enum;
using Sinacor.UI.Bovespa.Ordens.Communication.Bovespa.Cliente.Cliente; 
using Sinacor.Infra.Common.Validation;



namespace Sinacor.UI.Bovespa.Ordens.Communication
{
    public class Cliente : BaseCommunication
    {


        #region Construtor

        public Cliente(SessionInformation session)
            : base(session)
        {

        }

        #endregion

        #region ConsultarCliente

        public ModelCollection<Model.Cliente> ConsultarCliente(Model.Parameter.ConsultarClienteRequest pConsultarCliente)
        {
            ICliente servico = null;
            List<ClienteResponse> response = null;

            try
            {

                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    servico = serviceManager.TryGetInstance(this.Session);
                    response = servico.ConsultarCliente(new Translator.TranslatorConsultarClienteRequest().Translate(pConsultarCliente));
                }

                return new Translator.TranslatorCliente().TranslateToList(response);
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }
        }

        #endregion

        public ValidationResult ValidarCliente(Model.Parameter.ValidarClienteRequest pValidarCliente)
        {
            ICliente servico = null;
            ValidationResult result = null;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    servico = serviceManager.TryGetInstance(this.Session);
                    result = servico.ValidarCliente(new Translator.TranslatorValidarClienteParamenter().Translate(pValidarCliente));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }

            return result;
        }

        public Model.AgenteCustodia ConsultarAgenteCustodia(Model.Parameter.ConsultarClientePorCodigoRequest pConsultarCliente)
        {
            ICliente servico = null;
            AgenteCustodiaResponse response = null;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    servico = serviceManager.TryGetInstance(this.Session);
                    response = servico.ConsultarAgenteCustodia(new Translator.TranslatorConsultarClientePorCodigoRequest().Translate(pConsultarCliente));
                }

                return new Translator.TranslatorAgenteCustodiaResponse().Translate(response);
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }

        }

        public Model.UsuarioInstitucional ConsultarUsuarioInstitucional(Model.Parameter.ConsultarClientePorCodigoRequest pConsultarCliente)
        {
            ICliente servico = null;
            UsuarioInstitucionalResponse response = null;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    servico = serviceManager.TryGetInstance(this.Session);
                    response = servico.ConsultarUsuarioInstitucional(new Translator.TranslatorConsultarClientePorCodigoRequest().Translate(pConsultarCliente));
                }

                return new Translator.TranslatorUsuarioInstitucionalResponse().Translate(response);
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }


        }

        public ValidationResult ValidarDigitoVerifClienteInstitucional(Model.Parameter.ValidarDigitoVerifClienteInstitucionalRequest pValidarDigitoVerifClienteInstitucional)
        {
            ICliente servico = null;
            ValidationResult result = null;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    servico = serviceManager.TryGetInstance(this.Session);
                    result = servico.ValidarDigitoVerifClienteInstitucional(new Translator.TranslatorValidarDigitoVerifClienteInstitucional().Translate(pValidarDigitoVerifClienteInstitucional));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }

            return result;
        }

        /// <summary>
        /// Consulta os emissores de um cliente
        /// </summary>
        /// <param name="pConsultarEmissorOrdem"></param>
        /// <returns></returns>
        public ModelCollection<Model.EmissorOrdem> ConsultarEmissorOrdem(Model.Parameter.ConsultarEmissorOrdem pConsultarEmissorOrdem)
        {
            ICliente servico = null;
            List<EmissorOrdemResponse> response = null;

            try
            {

                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    servico = serviceManager.TryGetInstance(this.Session);
                    response = servico.ConsultarEmissorOrdem(new Translator.ConsultarEmissorOrdemToRequest().Translate(pConsultarEmissorOrdem));
                }

                return new Translator.EmissorOrdemResponseToModel().Translate(response);
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }
        }

        /// <summary>
        /// Verifica o bloqueio do cliente
        /// usado em: View.ManutencoaOrdem
        /// </summary>
        /// <param name="pVerificarBloqueioCliente"></param>
        /// <returns></returns>
        public Model.VerificarBloqueioCliente VerificarBloqueioCliente(Model.Parameter.VerificarBloqueioCliente pVerificarBloqueioCliente)
        {
            Model.VerificarBloqueioCliente objVerificarBloqueio = null;
            Bovespa.Cliente.Cliente.ICliente objICliente = null;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    objICliente = serviceManager.TryGetInstance(this.Session);
                    objVerificarBloqueio = new Translator.VerificarBloqueioClienteResponseToModel().Translate(objICliente.VerificarBloqueioCliente(new Translator.VerificarBloqueioClienteToRequest().Translate(pVerificarBloqueioCliente)));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

            return objVerificarBloqueio;
        }

        /// <summary>
        /// Verifica se o cliente é um broker
        /// </summary>
        /// <param name="pConsultarCliente"></param>
        /// <returns></returns>
        public bool ValidarClienteBroker(Model.Parameter.ConsultarClientePorCodigoRequest pConsultarCliente)
        {
            ICliente servico = null;
            bool result;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    servico = serviceManager.TryGetInstance(this.Session);
                    result = servico.ValidarClienteBroker(new Translator.TranslatorConsultarClientePorCodigoRequest().Translate(pConsultarCliente));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return false;
            }

            return result;
        }

        #region ValidarClienteApelido

        /// <summary>
        /// Data: 18/11/2008
        /// Autor: Valdenir Vanalli Filho (vvanalli.7comm)
        /// </summary>
        /// <param name="pConsultarClienteApelido"></param>
        /// <returns></returns>
        public Model.Cliente ConsultarClienteApelido(Model.Parameter.ConsultarClienteApelidoRequest pConsultarClienteApelido)
        {
            ICliente objServico = null;
            ClienteResponse response = null;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    objServico = serviceManager.TryGetInstance(this.Session);
                    response = objServico.ConsultarClienteApelido(new Translator.TranslatorConsultarClienteApelido().Translate(pConsultarClienteApelido));
                }

                return new Translator.TranslatorCliente().Translate(response);
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }
        }

        #endregion

        #region ValidarFilhote

        /// <summary>
        /// Data: 18/11/2008
        /// Autor: Valdenir Vanalli Filho (vvanalli.7comm)
        /// </summary>
        /// <param name="pValidarFilhote"></param>
        public void ValidarFilhote(Model.Parameter.ValidarFilhoteRequest pValidarFilhote)
        {
            ICliente objServico = null;

            try
            {
                using (ServiceInstance<ICliente> serviceManager = new ServiceInstance<ICliente>())
                {
                    objServico = serviceManager.TryGetInstance(this.Session);
                    objServico.ValidarFilhote(new Translator.TranslatorValidarFilhote().Translate(pValidarFilhote));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }
        #endregion
    }
}
